package org.chenzhi.mvp.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Fragment封装
 * @author orange
 */

public abstract class BaseFragment extends Fragment implements View.OnClickListener {

    private View view;
    private int layoutId;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        layoutId = getLayoutId();
        view = inflater.inflate(layoutId, container, false);
        init(view);
        return view;
    }

    protected abstract int getLayoutId();
    protected void init(View view) {

    }

    protected void setContent(int resId, String content) {
        TextView textView = (TextView) view.findViewById(resId);
        textView.setText(content);
    }

    protected String getContent(int resId) {
        TextView textView = (TextView) view.findViewById(resId);
        return textView.getText().toString();
    }

    protected void setClick(int... resIds) {
        for(int id : resIds) {
            view.findViewById(id).setOnClickListener(this);
        }
    }

    protected void processClick(int resId) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        view = null;
    }

    @Override
    public void onClick(View view) {
        processClick(view.getId());
    }

    private void jumpFragment(int resId, Fragment fragment) {
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(resId, fragment).commit();
    }

    public <E extends View> E findView(int resId) {
        return (E) view.findViewById(resId);
    }
}
